package com.foreknow.demo18;

import java.sql.*;

/**
 * 预处理 PreparedStatement接口
 * 什么是预处理：就是我们的SQL语句中有？(表示占位符)
 * Statement:它只能执行静态的SQL语句  例如:insert into emp(empno,ename,job) values(9999,'fender','music')
 * PreparedStatement:可以执行动态的SQL语句 例如：insert into emp(empno,ename,job) values(?,?,?)
 */
public class JdbcPrepareStatement {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/basic2","root","123456");
            PreparedStatement prepareStatement = conn.prepareStatement("insert into emp(empno,ename,job) values(?,?,?)");
            //一定要将？替换一个具体的值
            // void setInt(int parameterIndex,int x) parameterIndex表示第几个？  x表示要替换的值
            prepareStatement.setInt(1,8888);
            prepareStatement.setString(2,"gibson");
            prepareStatement.setString(3,"music");
            int isRligth = prepareStatement.executeUpdate();
            System.out.println(isRligth);
            //释放资源
            prepareStatement.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
